package com.minelx.sequence.pattern;

import com.minelx.sequence.core.ReturnMatcher;
import com.minelx.sequence.core.StringScanner;

import java.util.Map;

public class ConfigurableMatcher implements ReturnMatcher<ReturnMatcher<?, StringScanner>, StringScanner> {
	private final Map<String, ReturnMatcher<?, StringScanner>> configuredMatchers;

	public ConfigurableMatcher(Map<String, ReturnMatcher<?, StringScanner>> configuredMatchers) {
		this.configuredMatchers = configuredMatchers;
	}

	@Override
	public boolean isMatch(StringScanner clue) {
		StringScanner copied = clue.copy();
		if (!copied.pass("[")) {
			return false;
		}
		String matcherName = copied.nextPage();
		if (!configuredMatchers.containsKey(matcherName)) {
			return false;
		}
		return copied.exists("]");
	}

	@Override
	public ReturnMatcher<?, StringScanner> onMatched(StringScanner clue) {
		clue.goes("[");
		String matcherName = clue.nextPage();
		clue.goes("]");
		return configuredMatchers.get(matcherName);
	}
}
